<?php

namespace app\admin\model\shop\order;

use app\admin\model\shop\user\User;
use think\Model;
use think\model\relation\BelongsTo;
use think\model\relation\HasOne;

/**
 * Refund
 */
class Refund extends Model
{
    // 表名
    protected $name = 'shop_order_refund';

    protected $type = [
        'create_time'        => 'timestamp:Y-m-d H:i:s',
        'user_ship_time'     => 'timestamp:Y-m-d H:i:s',
        'receive_time'       => 'timestamp:Y-m-d H:i:s',
        'refund_address'     => 'json',
        'user_ship_delivery' => 'json',
        'exchange_delivery'  => 'json'
    ];

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    public function orderRelation(): BelongsTo
    {
        return $this->belongsTo(Order::class, 'order_id', 'id');
    }

    public function detail(): HasOne
    {
        return $this->hasOne(OrderDetail::class, 'id', 'order_detail_id');
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function getRemarkImagesAttr($value): array
    {
        if (!$value) {
            return [];
        }

        return array_map('full_url', explode(',', $value));
    }
}